const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

// Generate pages object
const pagesObj = {};

const chromeName = ["popup", "options"];

chromeName.forEach(name => {
  pagesObj[name] = {
    entry: `src/${name}/index.js`,
    template: "public/index.html",
    filename: `${name}.html`
  };
});

const plugins = [
  {
    from: path.resolve("src/manifest.json"),
    to: `${path.resolve("dist")}/manifest.json`
  },
  {
    from: './src/assets/images',
    to: 'static/images'
  }
]
//CopyWebpackPlugin   作用就是配置文件里需要用到的js，css，img文件完整拷贝到打包文件
module.exports = {
  pages: pagesObj,
  productionSourceMap: false,
  configureWebpack: {
    entry: {
      background: './src/options/background.js',
      content_script: './src/options/content_script.js',
      popup: './src/popup/index.js'
    },
    output: {
      filename: 'static/[name].js',
      path: path.resolve(__dirname, 'dist/')
    },
    plugins: [CopyWebpackPlugin(plugins)]
  },
  chainWebpack: config => {
    // 查看打包组件大小情况     
    if (process.env.npm_config_report) {
      // 在运行命令中添加 --report参数运行， 如：npm run build --report       
      config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    }
  }
};